package gemini.castor.ui.client.page.content.customer.customerlist.widget;

import gemini.basic.model.Customer;
import gemini.castor.ui.client.constants.I18nConstants;
import gemini.castor.ui.client.page.widget.dialog.GmnDialog;
import gemini.castor.ui.client.page.widget.dialog.GmnDialogEvent;
import gemini.castor.ui.client.page.widget.dialog.GmnDialogHandler;
import gemini.castor.ui.client.utils.GuiUtils;
import gemini.castor.ui.client.utils.HtmlUtils;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.event.shared.HasHandlers;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;
import com.google.gwt.user.client.ui.HTMLTable.RowFormatter;

public class CustomerTableWidget extends Composite implements HasHandlers {

	public CustomerTableWidget() {
		constants = GWT.create(I18nConstants.class);
		initTable();
		initHeader();
		initWidget(ftbSummary);
	}
	private I18nConstants constants;
	
	FlexTable ftbSummary;

	private void initTable() {
		ftbSummary = new FlexTable();
		ftbSummary.setWidth("100%");
		ColumnFormatter colFmt = ftbSummary.getColumnFormatter();

		colFmt.setWidth(0, "5%");
		colFmt.setWidth(1, "20%");
		colFmt.setWidth(2, "15%");
		colFmt.setWidth(3, "50%");
		colFmt.setWidth(4, "5%");
		colFmt.setWidth(5, "5%");
	}

	private void initHeader() {
		ftbSummary.setHTML(0, 0, HtmlUtils.bold(constants.htmOrderNumber()));
		ftbSummary.setHTML(0, 1, HtmlUtils.bold(constants.htmFullName()));
		ftbSummary.setHTML(0, 2, HtmlUtils.bold(constants.htmPhone()));
		ftbSummary.setHTML(0, 3, HtmlUtils.bold(constants.htmAddress()));
		ftbSummary.setHTML(0, 4, HtmlUtils.bold(constants.htmEdit()));
		ftbSummary.setHTML(0, 5, HtmlUtils.TAG_SPACE);
		ftbSummary.getRowFormatter().setStyleName(0, "tableHeader");
		ftbSummary.setStyleName("ftbSummary");
		
		
		HTML htmAdd = new HTML("<div id=\"btnAdd\">" + "&nbsp;"
				+ "</div>");
		htmAdd.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				fireEvent(new CustomerTableEvent());
			}
		});
		
		ftbSummary.setWidget(0, 5, htmAdd);
		
		ftbSummary.getRowFormatter().setStyleName(0, "tableHeader");
		ftbSummary.setStyleName("ftbSummary");
	}

	public void loadData(List<Customer> items) {

		ftbSummary.removeAllRows();
		initHeader();

		if (items != null && !items.isEmpty()) {
			initOrderTable();
			ftbSummary.removeAllRows();
			initHeader();
			RowFormatter rowFmt = ftbSummary.getRowFormatter();
			
			int i = 1;
			for (final Customer it : items) {
				if (i % 2 == 0) {
					rowFmt.setStyleName(i, "rowEven");
				}
				ftbSummary.setHTML(i, 0, i + "");
				ftbSummary.setHTML(i, 1, it.getPersons().get(0).getFirstName()+" "+it.getPersons().get(0).getLastName() );
				ftbSummary.setHTML(i, 2, it.getPersons().get(0).getPhoneNumber());
				ftbSummary.setHTML(i, 3, GuiUtils.addressToString(it.getPersons().get(0).getAddress()));
				HTML htmRemove = new HTML("<div id=\"btnDelete\">" + "&nbsp;"
						+ "</div>");
				htmRemove.addClickHandler(new ClickHandler() {

					@Override
					public void onClick(ClickEvent event) {
						confirmDelete(it);
					}
				});

				ftbSummary.setWidget(i, 5, htmRemove);
				
				HTML htmEdit = new HTML("<div id=\"btnEdit\">" + "&nbsp;"
						+ "</div>");
				htmEdit.addClickHandler(new ClickHandler() {

					@Override
					public void onClick(ClickEvent event) {
						fireEvent(new CustomerTableEvent(CustomerTableEvent.EventType.EDIT, it));
					}
				});
				ftbSummary.setWidget(i, 4, htmEdit);
				i++;
			}

		}
	}
	
	private void initOrderTable() {
		ColumnFormatter colFmt = ftbSummary.getColumnFormatter();

		colFmt.setWidth(0, "5%");
		colFmt.setWidth(1, "15%");
		colFmt.setWidth(2, "30%");
		colFmt.setWidth(3, "30%");
		colFmt.setWidth(4, "10%");
		colFmt.setWidth(5, "10%");
	}


	private void confirmDelete(final Customer it) {
		String message = constants.htmAreYouSure();

		GmnDialog dlgConfirm = new GmnDialog("Confirm", message, "Yes", "No");

		dlgConfirm.addConfirmationHandler(new GmnDialogHandler() {

			@Override
			public void onOK(GmnDialogEvent event) {
				fireEvent(new CustomerTableEvent(CustomerTableEvent.EventType.DELETE, it));
			}

			@Override
			public void onCancel(GmnDialogEvent event) {

			}
		});

		dlgConfirm.show();
	}

	public HandlerRegistration addEventsActionHandler(CustomerTableHandler handler) {
		return addHandler(handler, CustomerTableEvent.getType());
	}
}
